package pub.tbc.atps.process.validator;

import lombok.extern.slf4j.Slf4j;
import pub.tbc.atps.annotation.HandlerOrder;
import pub.tbc.atps.process.Handler;
import pub.tbc.atps.process.HandlerContext;
import pub.tbc.atps.task.HttpMethod;
import pub.tbc.atps.task.ParamType;
import pub.tbc.atps.task.RestTask;
import pub.tbc.toolkit.Objs;

/**
 * @author tbc on 2016/11/29 13:20:24.
 */
@Slf4j
@HandlerOrder(1)
public class Validator implements Handler {
    public boolean handler(HandlerContext context) {
        RestTask task = context.getAttribute("task");
        try {
            HttpMethod.getHttpMethod(task.getHttpMethod());
        } catch (Exception e) {
            log.error("HttpMethod不正常： {}", e.getMessage());
            return false;
        }
        if (Objs.isEmpty(task))
            return bad("任务为空，终止任务： task = ", task);
        if (Objs.isEmpty(task.getRequestId()))
            return bad("请求ID为空，终止任务： request= ", task.getRequestId());

        if (checkUrl(task.getUrl())) {
            log.info("任务校验通过");
            return true;
        } else {
            return false;
        }
    }

    private boolean checkUrl(String url) {
        if (Objs.isEmpty(url))
            return false;
        // 'http://'开头,
        return true;
    }

    private boolean bad(String msg, Object object) {
        log.error("{} {}", msg, object);
        return false;
    }
}
